package com.innerchic.apt.tool;

import com.innerchic.annotations.base.Skip;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：young
 * 日    期：2021/11/1 15:32
 * 包    名：com.innerchic.apt.tool
 * 描    述：基础的Processor类
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */
public abstract class BaseAbstractProcessor extends AbstractProcessor {

    protected Messager baseMessage;
    protected Elements baseElements;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        baseMessage = processingEnv.getMessager();
        baseElements = processingEnv.getElementUtils();
    }

    protected void logPut(String log) {
        baseMessage.printMessage(Diagnostic.Kind.NOTE, log);
    }

    /**
     * 当前方法获取的Element可以使Skip注解生效
     *
     * @param roundEnvironment 所有的Element集合
     * @return RoundEnvironment.getElementsAnnotatedWith
     */
    public Set<? extends Element> getElementsAnnotatedWith(RoundEnvironment roundEnvironment, Class<? extends Annotation> var1) {
        Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(var1);
        Set<Element> news = new HashSet<>();
        for (Element element : elements) {
            Skip annotation = element.getAnnotation(Skip.class);
            if (annotation == null) {
                news.add(element);
            }
        }
        return news;
    }

}
